package com.abbas.test230430.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.content.res.AppCompatResources;

import com.abbas.test230430.R;
import com.abbas.test230430.entity.TransactionEntity;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

public class TransactionTableLayout extends TableLayout {

    List<TransactionEntity> data = Collections.emptyList();
    OnItemSettingClickListener listener;

    public TransactionTableLayout(Context context) {
        super(context);
        init();
    }

    public TransactionTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        Context context = getContext();
        setBackgroundColor(getResources().getColor(R.color.gray1));


        TableRow titleRow = getRow();

        TextView lk = new TransactionTextView(context, true);
        lk.setText("路口");
        titleRow.addView(lk);

        TransactionTextView hongdsc = new TransactionTextView(context, true);
        hongdsc.setText("红灯时长");
        titleRow.addView(hongdsc);

        TransactionTextView huangdsc = new TransactionTextView(context, true);
        huangdsc.setText("黄灯时长");
        titleRow.addView(huangdsc);

        TransactionTextView ldsc = new TransactionTextView(context, true);
        ldsc.setText("绿灯时长");
        titleRow.addView(ldsc);

        TransactionTextView czx = new TransactionTextView(context, true);
        czx.setText("操作项");
        titleRow.addView(czx);

        TransactionTextView sz = new TransactionTextView(context, true);
        sz.setText("设置");
        titleRow.addView(sz);

        setStretchAllColumns(true);

        addView(titleRow);
    }

    public void addRows(List<TransactionEntity> data) {
        removeAllViews();
        init();

        this.data = data;

        ListIterator<TransactionEntity> transactionEntityListIterator = this.data.listIterator();
        while (transactionEntityListIterator.hasNext()) {
            TableRow row = getRow();

            int nextIndex = transactionEntityListIterator.nextIndex();
            TransactionEntity datum = transactionEntityListIterator.next();

            TransactionTextView lk = new TransactionTextView(getContext(), false);
            lk.setText(String.valueOf(datum.getLk()));
            row.addView(lk);

            TransactionTextView hongdsc = new TransactionTextView(getContext(), false);
            hongdsc.setText(String.valueOf(datum.getHongdsc()));
            row.addView(hongdsc);

            TransactionTextView huangdsc = new TransactionTextView(getContext(), false);
            huangdsc.setText(String.valueOf(datum.getHuangdsc()));
            row.addView(huangdsc);

            TransactionTextView ldsc = new TransactionTextView(getContext(), false);
            ldsc.setText(String.valueOf(datum.getLdsc()));
            row.addView(ldsc);

//            CheckBox checkBox = new CenterCheckBox(getContext());
//            checkBox.setGravity(Gravity.CENTER);
//            checkBox.setButtonDrawable(null);
//            checkBox.setCompoundDrawables(AppCompatResources.getDrawable(getContext(),R.drawable.check_selector),null,null,null);
//            checkBox.setBackgroundColor(getResources().getColor(R.color.purple_200));
//            row.addView(checkBox);

            CheckBox checkBox = new CheckBox(getContext());
            row.addView(checkBox);


            LinearLayout settingContainer = new LinearLayout(getContext());
            settingContainer.setGravity(Gravity.CENTER);
            TextView settingText = new TextView(getContext());
            settingText.setClickable(true);
            settingText.setGravity(Gravity.CENTER);
            settingText.setBackground(AppCompatResources.getDrawable(getContext(), R.drawable.text_bt));
            settingText.setPadding(4, 4, 4, 4);
            settingText.setText("设置");

            settingText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(nextIndex, datum);
                    }
                }
            });

            settingContainer.addView(settingText);
            row.addView(settingContainer);

            addView(row);
        }

        requestLayout();
    }

    public void sort(int type) {
        switch (type) {
            case 0:
                data = data.stream()
                        .sorted(Comparator.comparingInt(TransactionEntity::getLk))
                        .collect(Collectors.toList());
                break;
            case 1:
                data = data.stream()
                        .sorted((o1, o2) -> o2.getLk() - o1.getLk())
                        .collect(Collectors.toList());
                break;
            default: //writer other in above there
                break;
        }
        addRows(data);
    }

    private TableRow getRow() {
        TableRow row = new TableRow(getContext());

        row.setGravity(Gravity.CENTER);
        row.setBackground(AppCompatResources.getDrawable(getContext(), R.drawable.transaction_row_shape));
        row.setOrientation(LinearLayout.HORIZONTAL);
        row.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        row.setDividerDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.white_shape));
        return row;
    }

    public interface OnItemSettingClickListener {
        void onClick(int position, TransactionEntity data);
    }

    public OnItemSettingClickListener getListener() {
        return listener;
    }

    public void setListener(OnItemSettingClickListener listener) {
        this.listener = listener;
    }
}
